﻿using System;
using System.Data;
using System.Text;

namespace ltModel.DAL
{
    /// <summary>
    /// 数据访问类article_user。
    /// </summary>
    public class article_user
    {
        public article_user()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from lt_article_user where user_id=" + user_id + "");
            return DataObject.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ltModel.Model.article_user model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into lt_article_user(");
            strSql.Append("username,userpass,is_man,regdate,lasttime,fb_num,sh_num,regip,truename,sex,oicq,email,homepage,jm,lmqx,funcqx,lock,role_id");
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append("'" + model.username + "',");
            strSql.Append("'" + model.userpass + "',");
            strSql.Append("'" + model.is_man + "',");
            strSql.Append("'" + model.regdate + "',");
            strSql.Append("'" + model.lasttime + "',");
            strSql.Append("" + model.fb_num + ",");
            strSql.Append("" + model.sh_num + ",");
            strSql.Append("'" + model.regip + "',");
            strSql.Append("'" + model.truename + "',");
            strSql.Append("" + model.sex + ",");
            strSql.Append("'" + model.oicq + "',");
            strSql.Append("'" + model.email + "',");
            strSql.Append("'" + model.homepage + "',");
            strSql.Append("'" + model.jm + "',");
            strSql.Append("'" + model.lmqx + "',");
            strSql.Append("'" + model.funcqx + "',");
            strSql.Append("'" + model.Lock + "',");
            strSql.Append("" + model.role_id + "");
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DataObject.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ltModel.Model.article_user model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_article_user set ");
            if (model.username != null)
                strSql.Append("username='" + model.username + "',");
            if (model.userpass != null)
                strSql.Append("userpass='" + model.userpass + "',");
            if (model.is_man != null)
                strSql.Append("is_man='" + model.is_man + "',");
            if (model.lasttime != null)
                strSql.Append("lasttime='" + model.lasttime + "',");
            if (model.fb_num.Equals(Int32.Parse("0")) != true)
                strSql.Append("fb_num=" + model.fb_num + ",");
            if (model.sh_num.Equals(Int32.Parse("0")) != true)
                strSql.Append("sh_num=" + model.sh_num + ",");
            if (model.truename != null)
                strSql.Append("truename='" + model.truename + "',");
            if (model.sex != null)
                strSql.Append("sex=" + model.sex + ",");
            if (model.oicq != null)
                strSql.Append("oicq='" + model.oicq + "',");
            if (model.email != null)
                strSql.Append("email='" + model.email + "',");
            if (model.homepage != null)
                strSql.Append("homepage='" + model.homepage + "',");
            if (model.jm != null)
                strSql.Append("jm='" + model.jm + "',");
            if (model.lmqx != null)
                strSql.Append("lmqx='" + model.lmqx + "',");
            if (model.funcqx != null)
                strSql.Append("funcqx='" + model.funcqx + "',");
            if (model.Lock != null)
                strSql.Append("lock='" + model.Lock + "',");
            if (model.role_id.Equals(Int32.Parse("0")) != true)
                strSql.Append("role_id=" + model.role_id + ",");
            strSql.Append(" regip=regip ");
            strSql.Append(" where user_id=" + model.user_id + "");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 锁定解锁用户
        /// </summary>
        /// <param name="user_id">用户</param>
        /// <param name="lk">状态</param>
        public void LockEdit(string user_id, string lk)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update lt_article_user set ");
            strSql.Append(" lock= '" + lk + "' ");
            if (user_id.Trim() != "")
                strSql.Append(" where user_id in (" + user_id + ") ");
            DataObject.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_article_user ");
            strSql.Append(" where user_id=" + user_id);
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="user_id">删除ID</param>
        /// <returns></returns>
        public bool Delete(string user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete lt_article_user ");
            strSql.Append(" where user_id in(" + user_id + ") ");
            if (DataObject.ExecuteSql(strSql.ToString()) > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ltModel.Model.article_user GetModel(int user_id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  ");
            strSql.Append(" [user_id],[username],[userpass],[is_man],[regdate],[lasttime],[fb_num],[sh_num],[regip],[truename],[sex],[oicq],[email],[homepage],[jm],[lmqx],[funcqx],[lock],[role_id] ");
            strSql.Append(" from lt_article_user ");
            strSql.Append(" where user_id=" + user_id);
            ltModel.Model.article_user model = new ltModel.Model.article_user();
            DataSet ds = DataObject.Query(strSql.ToString());
            model.user_id = user_id;
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.username = ds.Tables[0].Rows[0]["username"].ToString();
                model.userpass = ds.Tables[0].Rows[0]["userpass"].ToString();
                model.is_man = ds.Tables[0].Rows[0]["is_man"].ToString();
                model.regdate = ds.Tables[0].Rows[0]["regdate"].ToString();
                model.lasttime = ds.Tables[0].Rows[0]["lasttime"].ToString();
                if (ds.Tables[0].Rows[0]["fb_num"].ToString() != "")
                {
                    model.fb_num = int.Parse(ds.Tables[0].Rows[0]["fb_num"].ToString());
                }
                if (ds.Tables[0].Rows[0]["sh_num"].ToString() != "")
                {
                    model.sh_num = int.Parse(ds.Tables[0].Rows[0]["sh_num"].ToString());
                }
                model.regip = ds.Tables[0].Rows[0]["regip"].ToString();
                model.truename = ds.Tables[0].Rows[0]["truename"].ToString();
                if (ds.Tables[0].Rows[0]["sex"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["sex"].ToString() == "1") || (ds.Tables[0].Rows[0]["sex"].ToString().ToLower() == "true"))
                    {
                        model.sex = "1";
                    }
                    else
                    {
                        model.sex = "0";
                    }
                }

                model.oicq = ds.Tables[0].Rows[0]["oicq"].ToString();
                model.email = ds.Tables[0].Rows[0]["email"].ToString();
                model.homepage = ds.Tables[0].Rows[0]["homepage"].ToString();
                model.jm = ds.Tables[0].Rows[0]["jm"].ToString();
                model.lmqx = ds.Tables[0].Rows[0]["lmqx"].ToString();
                model.funcqx = ds.Tables[0].Rows[0]["funcqx"].ToString();
                model.Lock = ds.Tables[0].Rows[0]["lock"].ToString();
                if (ds.Tables[0].Rows[0]["role_id"].ToString() != "")
                {
                    model.role_id = int.Parse(ds.Tables[0].Rows[0]["role_id"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select [user_id],[username],[userpass],[is_man],[regdate],[lasttime],[fb_num],[sh_num],[regip],[truename],[sex],[oicq],[email],[homepage],[jm],[lmqx],[funcqx],[lock],[role_id] ");
            strSql.Append(" FROM lt_article_user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string topNum, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (PageValidate.IsNumber(topNum))
                strSql.Append(" top " + topNum + " ");
            strSql.Append(" [user_id],[username],[userpass],[is_man],[regdate],[lasttime],[fb_num],[sh_num],[regip],[truename],[sex],[oicq],[email],[homepage],[jm],[lmqx],[funcqx],[lock],[role_id] ");
            strSql.Append(" FROM lt_article_user ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DataObject.Query(strSql.ToString());
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "article_user";
            parameters[1].Value = "user_id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DataObject.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  成员方法
    }
}